/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.persistence;

import java.io.Serializable;
import javax.persistence.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * The persistent class for the stock_types database table.
 * 
 */
@Entity
@Table(name="stock_types")
public class StockType implements Serializable, AugeasWHEntity {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name="class")
	private String productClass;

	private String name;

	private BigDecimal price;

	//bi-directional many-to-one association to Discount
	@OneToMany(mappedBy="stockType")
	private List<Discount> discounts;

	//bi-directional many-to-one association to OrdersStockType
	@OneToMany(mappedBy="stockType")
	private List<OrdersStockType> ordersStockTypes;

	//bi-directional many-to-one association to StockItem
	@OneToMany(mappedBy="stockType")
	private List<StockItem> stockItems;

    public StockType() {
    }

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getProductClass() {
		return this.productClass;
	}

	public void setProductClass(String productClass) {
		this.productClass = productClass;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public List<Discount> getDiscounts() {
		return this.discounts;
	}

	public void setDiscounts(List<Discount> discounts) {
		this.discounts = discounts;
	}
	
	public List<OrdersStockType> getOrdersStockTypes() {
		return this.ordersStockTypes;
	}

	public void setOrdersStockTypes(List<OrdersStockType> ordersStockTypes) {
		this.ordersStockTypes = ordersStockTypes;
	}
	
	public List<StockItem> getStockItems() {
		return this.stockItems;
	}

	public void setStockItems(List<StockItem> stockItems) {
		this.stockItems = stockItems;
	}
	
	@Override
	public boolean equals(Object other) {
		if (other == null) return false;
		if (other == this) return true;
		if (!(other instanceof StockType)) return false;
		StockType stOther = (StockType) other;
		return (stOther.getName().equals(getName()) && stOther.getId() == getId());
	}
	
	public BigDecimal getTotalAmount() {
		BigDecimal amount = new BigDecimal(0);
		
		for (StockItem item : getStockItems()){
			for (SlotsStockItem slot : item.getSlotsStockItems()){
				amount = amount.add(slot.getAmmount());
			}
		}
		return amount;
	}
}